package com.zeng.mvpdemo.ui;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Toast;

import com.zeng.mvpdemo.presenter.BasePresenter;
import com.zeng.mvpdemo.view.IBaseView;

public abstract class MvpBaseActivity<K extends IBaseView, P extends BasePresenter<K>> extends Activity implements IBaseView {

    protected ProgressDialog mProgressDialog;
    protected P mPresenter;
//
//    protected P getPresenter() {
//        return mPresenter;
//    }

    protected abstract P createPresenter();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mProgressDialog = new ProgressDialog(this);
        if (mPresenter == null) {
            mPresenter = createPresenter();
        }
        if (mPresenter != null) {
            mPresenter.attach((K) this);
        }
    }

    public void onProgressBarVisibility(boolean pShown, String pMsg) {
        if (pShown) {
            mProgressDialog.setMessage(pMsg);
            mProgressDialog.show();
        } else {
            mProgressDialog.dismiss();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
    }

    @SuppressWarnings("unchecked")
    public final <T extends View> T getView(int id) {
        try {
            return (T) findViewById(id);
        } catch (ClassCastException e) {
            throw e;
        }
    }


    public void toast(String pStr) {
        Toast.makeText(this, pStr, Toast.LENGTH_SHORT).show();
    }
}
